package com.example.myapplication

import android.Manifest
import android.content.pm.PackageManager
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Scaffold
import androidx.compose.ui.Modifier
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import com.example.myapplication.ui.theme.MyApplicationTheme
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class MainActivity : ComponentActivity() {
    private lateinit var mockLocationHelper: MockLocationHelper
    private lateinit var dbHelper: HistoryDatabaseHelper

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        mockLocationHelper = MockLocationHelper(this)
        dbHelper = HistoryDatabaseHelper(this)

        // Request necessary permissions
        requestPermissions()

        // Load history from database
        CoroutineScope(Dispatchers.IO).launch {
            val history = dbHelper.getAllHistory()
            withContext(Dispatchers.Main) {
                setContent {
                    MyApplicationTheme {
                        Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
                            MainScreen(
                                modifier = Modifier.padding(innerPadding),
                                onStartClick = { lat, lon ->
                                    mockLocationHelper.startMocking(lat, lon)
                                },
                                onEndClick = {
                                    mockLocationHelper.stopMocking()
                                },
                                initialHistory = history,
                                onSaveHistory = { lat, lon ->
                                    CoroutineScope(Dispatchers.IO).launch {
                                        if (!dbHelper.historyExists(lat, lon)) {
                                            dbHelper.insertHistory(lat, lon)
                                        }
                                    }
                                },
                                onDeleteHistory = { id ->
                                    CoroutineScope(Dispatchers.IO).launch {
                                        dbHelper.deleteHistory(id)
                                    }
                                }
                            )
                        }
                    }
                }
            }
        }
    }

    private fun requestPermissions() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED ||
            ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, arrayOf(
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.ACCESS_COARSE_LOCATION
            ), 1)
        }
    }
}
